package com.rensea.android.api;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import com.rensea.android.objs.Message;

public class FavoritesAPIClient extends AbstractAPIClient {
	public static final String FAVORITES_SERVICE = Server+"/favorites/list.json";
	public static final String FAVORITES_ADD_SERVICE = Server+"/favorites/create.json";
	public static final String FAVORITES_REMOVE_SERVICE = Server+"/favorites/destroy.json";
	public static boolean addOrRemove(Message msg){
		if(!isLogin())return false;
		HttpClient httpClient = getHttpClientWithCookie(); 
		try {
			HttpPost httpget = new HttpPost(msg.isFavorited()?FAVORITES_REMOVE_SERVICE:FAVORITES_ADD_SERVICE);
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

			nameValuePairs.add(new BasicNameValuePair("id", msg.getId()+""));

			httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));

			HttpResponse resp = httpClient.execute(httpget);
			String result = getContent(resp);
			logD(result);
//			System.out.println(result);
			return true;
			
		}catch (Exception e) {
			e.printStackTrace();
		}				
		
		
		return false; 
	}
}
